﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ORM.Business.Feedbacks;
using ORM.Business.Interfaces;
using ORM.Business.Models.Entities;
using ORM.Common.Logging;
using ORM.Resources;

namespace ORM.Business.Repositories
{
    public class ManufacturerRepository : IManufacturerRepository
    {

        //-------------------------------------------------------------------------------------------------
        public Feedback<IList<Manufacturer>> GetAllManufacturer()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    var manufacturer =
                        entities.Manufacturers.Where(t => !t.IsDeleted).OrderBy(t => t.Name).ToList();
                    return new Feedback<IList<Manufacturer>>(true, null, manufacturer);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<IList<Manufacturer>>(false, Messages.GeneralError);
            }
        }

        public Feedback DeleteManufacturerCategory(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    foreach (var model in entities.Models.Where(i => i.ManufacturerId == id).ToList())
                    {
                        model.IsDeleted = true;
                        foreach (var guide in entities.Guides.Where(i => i.ModelID == model.Id).ToList())
                        {
                            guide.IsDeleted = true;
                        }
                        foreach (var question in entities.Questions.Where(i => i.ModelId == model.Id).ToList())
                        {
                            question.IsDeleted = true;
                        }
                    }
                    Manufacturer c = entities.Manufacturers.Single(i => i.Id == id);
                    c.IsDeleted = true;
                    entities.SaveChanges();

                    return new Feedback(true);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback(false, Messages.GeneralError);
            }
        }

        public Feedback<Manufacturer> InsertManufacturer(Manufacturer t)
        {
            Manufacturer tnew = new Manufacturer
            {
                Name = t.Name,
                Description =  t.Description,
                IsDeleted = false
            };

            try
            {
                using (var entities = new OrmEntities())
                {
                    //Check for duplicate name
                    var exist = entities.Manufacturers
                        .FirstOrDefault(i => i.Name == tnew.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<Manufacturer>(false, Messages.InsertCategory_DuplicateName);

                    entities.Manufacturers.Add(tnew);
                    
                    entities.SaveChanges();                   
                }
                return new Feedback<Manufacturer>(true, Messages.InsertManufacturerCategorySuccess, tnew);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Manufacturer>(false, Messages.GeneralError);
            }
        }

        public Feedback<Manufacturer> UpdateManufacturerCategory(Manufacturer c)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    // Check for duplicate name
                    Manufacturer exist = entities.Manufacturers.FirstOrDefault(i => i.Id != c.Id && i.Name == c.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<Manufacturer>(false, Messages.InsertCategory_DuplicateName);

                    var category = entities.Manufacturers.Single(i => i.Id == c.Id);
                    category.Name = c.Name;
                    category.Description = c.Description;
                    entities.SaveChanges();

                    return new Feedback<Manufacturer>(true, null, category);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Manufacturer>(false, Messages.GeneralError);
            }
        }

        public Feedback<Manufacturer> GetManufacturerCategoryDetails(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    Manufacturer category = entities.Manufacturers.SingleOrDefault(i => i.Id == id && !i.IsDeleted);
                    if (category != null)
                        return new Feedback<Manufacturer>(true, null, category);
                    else
                        return new Feedback<Manufacturer>(false, Messages.GetCategory_NotFound);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Manufacturer>(false, Messages.GeneralError);
            }
        }

    }
}